package com.yyh.controller;


import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyh.entity.Modules;
import com.yyh.entity.Rolemodules;
import com.yyh.entity.UsersParam;
import com.yyh.service.IModulesService;
import com.yyh.service.IRolemodulesService;
import com.yyh.util.Result;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangyueheng
 * @since 2020-06-02
 */
@RestController
@RequestMapping("/modules")
public class ModulesController {
	@Autowired
	IModulesService imodulesService;
	@Autowired
	IRolemodulesService irolemodulesService;
	
	 @RequestMapping("/addModule")
	    public Object addModule(@Valid Modules modules){
	        boolean save = imodulesService.save(modules);
	        if(save) {
	            return new Result(0,"添加成功");
	        }else {
	            return new Result(1,"添加失败");
	        }
	    }

	    @RequestMapping("/updateModule")
	    public Object updateModule(@RequestParam Integer id, @Valid Modules modules){
	        modules.setId(id);
	        boolean update = imodulesService.updateById(modules);
	        if(update) {
	            return new Result(0,"修改成功");
	        }else {
	            return new Result(1,"修改失败");
	        }
	    }

	    @RequestMapping("/delModule")
	    public Object delModule(@RequestParam Integer id) {
	        boolean remove = imodulesService.removeById(id);
	        if(remove) {
	            return new Result(0,"删除成功");
	        }else {
	            return new Result(1,"删除失败");
	        }
	    }
	    
	    @RequestMapping("/getList")
		public Object getList(UsersParam p) {
			//分页-需要分页的配置文件
			Page<Modules> page = new Page<>(p.getPage(),p.getLimit());
			
			//多条件
			QueryWrapper<Modules> queryWrapper = new QueryWrapper<Modules>();
			IPage<Modules> ipage = imodulesService.page(page, queryWrapper);
			
			List<Modules> data = ipage.getRecords();
			long total = ipage.getTotal();
			return new Result(0,"",data,total);
		}
	    @RequestMapping("/getModulesTree")
		public Object getModulesTree() {
			List<Modules> list = imodulesService.getModulesTree();
			return list;
		}
	    @PostMapping("/getModulesTreeByRoleId")
		public Object getModulesTreeByRoleId(@RequestParam Integer rid) {
			List<Modules> list = imodulesService.getModulesTreeByRoleId(rid);
			return list;
		}
	    /**
		 * 给指定角色设置拥有的模块
		 * @param rid  角色id
		 * @param mids   模块id组成的字符串
		 * @return
		 */
		@PostMapping("/setModuleIdsByRoleId")
		public Object setModuleIdsByRoleId(@RequestParam Integer rid,@RequestParam String mids) {
			//删除该角色的所有数据（中间表）
			QueryWrapper<Rolemodules> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("role_id", rid);
			irolemodulesService.remove(queryWrapper);
			
			//给指定的角色批量添加数据
			List<Rolemodules> list = new ArrayList<>();
			String[] arr = mids.split(",");
			for(String s:arr) {
				list.add(new Rolemodules(0,rid,Integer.valueOf(s)));
			}
			//调用方法，批量添加集合中的数据
			boolean res = irolemodulesService.saveBatch(list);
			if(res) {
				return new Result(0,"设置成功");
			}else {
				return new Result(1,"设置失败");
			}
		}
		/**
		 * 获取指定用户的模块树
		 * @return
		 */
		@PostMapping("/getModulesTreeByUserId")
		public Object getModulesTreeByUserId(@RequestParam Integer uid) {
			List<Modules> list = imodulesService.getModulesTreeByUserId(uid);
			return list;
		}
}